function Z = remove_outliers(Z)
% Removes outlying scores by setting them to NaN
%
% Z: a matrix of scores, where the rows are subjects and columns are sentences

[N,M] = size(Z);

fprintf('Started with %d workers and %d scores.\n', size(Z,1), sum(~isnan(Z(:))));

mu = nanmean(Z);	% MOS for each sentence
s  = nanstd(Z);		% std dev for each sentence

mu_norm = abs(Z - repmat(mu,[N 1])) ./ repmat(s,[N 1]);		% normalized scores
outlying_scores  = (mu_norm > 3.0);
outlying_workers = (sum(outlying_scores,2) > .05*sum(~isnan(Z),2));

Z(outlying_scores) = NaN;		% remove scores which are more than 2.5 std devs away from the mean
Z = Z(~outlying_workers,:);		% remove subjects which have more than 5% of outlying scores

fprintf('Removed %d outlying scores.\n', sum(outlying_scores(:)));
fprintf('Removed %d outlying workers.\n', sum(outlying_workers(:)));

fprintf('Finished with %d workers and %d scores.\n\n', size(Z,1), sum(~isnan(Z(:))));

